{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "68b13acf-9326-42d8-b5f5-e96a7d1e2be4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai \n",
    "import os\n",
    "import tiktoken\n",
    "\n",
    "\n",
    "openai.api_key = os.environ[\"OPENAI_API_KEY\"]\n",
    "openai.api_base = \"https://api.chatanywhere.com.cn/v1\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6f992906-ff9a-4e5c-a77d-0a65ecc223f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_completion(prompt,model = \"gpt-3.5-turbo\"):\n",
    "    messages = [{\"role\" : \"user\", \"content\": prompt}]   # 我估计是代码哪里出现了问题，简直离大谱，不知道浪费了我多少时间\n",
    "    response = openai.ChatCompletion.create(\n",
    "        model = model,\n",
    "        messages = messages,\n",
    "        temperature = 0,\n",
    "    )\n",
    "    return response.choices[0].message[\"content\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c9e43bec-3e7e-45c8-9659-579d0da222b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = get_completion(\"What is the capital of China?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "60219711-6146-4574-aa37-f26a92b8087c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The capital of China is Beijing.\n"
     ]
    }
   ],
   "source": [
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c693ee1a-68b0-44c2-a5fe-076c19c2eb4f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
